cc.Class({
    extends: cc.Component,

    properties: {
        numNodes: [cc.Node],
        flowerNodes: [cc.Node],
        bigFlowerNode: cc.Node,
    },

    // use this for initialization
    onLoad: function () {
        // test
        this.testIdList = [];
        for (var i = 0; i < 4; i ++) {
            for (var j = 0; j < 13; j ++) {
                this.testIdList.push(i * 100 + j);
            }
        }
        this.testIdx = 0;
        this.schedule(()=>{
            this.show(this.testIdList[this.testIdx]);
            this.testIdx ++;
            if (this.testIdx >= this.testIdList.length) {
                this.testIdx = 0;
            }
        }, 1);
    },

    getIndexesById: function (id) {
        var cardId = id % 100;
        switch (cardId) {
            case 0: return [1,4,7];
            case 1: return [0,2,6,8];
            case 2: return [0,2,4,6,8];
            case 3: return [0,2,3,4,6,8];
            case 4: return [0,2,3,4,5,6,8];
            case 5: return [0,1,2,3,5,6,7,8];
            case 6: return [0,1,2,3,4,5,6,7,8];
            case 7: return [0,1,2,3,4,5,6,7,8];
            case 11: return [4];
            case 12: return [1,7];
            default:
                return [];
        }
    },

    getNumNameById: function (id) {
        var colorId = Math.floor(id/100);
        var cardId = id % 100;
        var colorS = "2";
        if (colorId == 0 || colorId == 3) {
            colorS = "1";
        }
        var numString = "PokerIndex" + cardId + colorS;
        return numString;
    },

    getFlowerNameById: function (id) {
        var colorId = Math.floor(id/100);
        return "pokerColor" + colorId;
    },

    show: function (id) {
        var numName = this.getNumNameById(id);
        var flowerName = this.getFlowerNameById(id);
        var indexes = this.getIndexesById(id);
        cc.loader.loadRes("UI/poker", cc.SpriteAtlas, (err, atlas)=>{
            var frame = atlas.getSpriteFrame(flowerName);
            for (var i = 0; i < 9; i ++) {
                let idx = i;
                if (indexes.indexOf(idx) != -1) {
                    this.flowerNodes[idx].getComponent(cc.Sprite).spriteFrame = frame;
                }
                else {
                    this.flowerNodes[idx].getComponent(cc.Sprite).spriteFrame = null;
                }
            }
        });

        cc.loader.loadRes("UI/poker", cc.SpriteAtlas, (err, atlas)=>{
            var frame = atlas.getSpriteFrame(numName);
            this.numNodes.forEach((node)=>{
                node.getComponent(cc.Sprite).spriteFrame = frame;
            });
        });

        var cardId = id % 100;
        if (cardId >= 8 && cardId <= 10) {
            cc.loader.loadRes("UI/poker", cc.SpriteAtlas, (err, atlas)=>{
                var frame = atlas.getSpriteFrame('dawang');
                this.bigFlowerNode.getComponent(cc.Sprite).spriteFrame = frame;
            });
        }
        else {
            this.bigFlowerNode.getComponent(cc.Sprite).spriteFrame = null;
        }

    },

    // called every frame, uncomment this function to activate update callback
    // update: function (dt) {

    // },
});
